package com.server.file.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.io.IoUtil;
import com.server.model.all.file.vo.UpLoadImageVo;
import com.server.model.all.file.vo.UpLoadMainAndThumbnailImageVo;
import com.server.model.all.file.vo.UploadFileVo;
import com.server.model.all.enums.BucketNameEnum;
import com.server.common.code.error.BusinessException;
import com.server.common.code.utils.UrlUtils;
import com.server.commons.file.interfaces.FileFactoryInterface;
import com.server.commons.file.interfaces.FileInterface;
import com.server.commons.file.model.input.UpLoadImageInput;
import com.server.commons.file.model.input.UploadFileInput;
import com.server.commons.file.model.output.UpLoadImageOutput;
import com.server.commons.file.model.output.UploadFileOutput;
import com.server.commons.rabbitmq.MyRabbitTemplate;
import com.server.file.service.FileUpLoadService;
import com.server.file.service.UploadImageLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

@Slf4j
@Service
public class FileUpLoadServiceImpl implements FileUpLoadService {
    @Resource
    private FileFactoryInterface fileFactoryInterface;

    @Resource
    private UploadImageLogService uploadImageLogService;

    @Resource
    private MyRabbitTemplate rabbitTemplate;

    @Override
    public UploadFileVo uploadFile(UploadFileInput uploadFileInput) {
        FileInterface fileInterface = fileFactoryInterface.getFileInterface(uploadFileInput.getBucketNameEnum());
        UploadFileOutput uploadFileOutput = fileInterface.uploadFile(uploadFileInput);
        return BeanUtil.copyProperties(uploadFileOutput,UploadFileVo.class);
    }

    @Override
    public UpLoadMainAndThumbnailImageVo upLoadMainAndThumbnailImage(UploadFileInput uploadFileInput) {
        UrlUtils.checkImageUrl(Arrays.asList(uploadFileInput.getSuffix()));
        try{
            //
            UpLoadImageVo upLoadImageVo = upLoadMainImage(uploadFileInput);
            //
            UpLoadImageVo upLoadImageFluencyVo = upLoadThumbnailImage(uploadFileInput,500);
            //
            UpLoadImageVo upLoadImageClearnessVo = upLoadThumbnailImage(uploadFileInput,1000);
            //
            UpLoadImageVo upLoadImageHighDefinitionVo = upLoadThumbnailImage(uploadFileInput,2000);
            //
            UpLoadImageVo upLoadImageSuperDefinitionVo = upLoadThumbnailImage(uploadFileInput,3000);
            //
            UpLoadMainAndThumbnailImageVo upLoadMainAndThumbnailImageVo = new UpLoadMainAndThumbnailImageVo();
            upLoadMainAndThumbnailImageVo.setMainImage(upLoadImageVo);
            upLoadMainAndThumbnailImageVo.setThumbnailImageFluency(upLoadImageFluencyVo);
            upLoadMainAndThumbnailImageVo.setThumbnailImageClearness(upLoadImageClearnessVo);
            upLoadMainAndThumbnailImageVo.setThumbnailImageHighDefinition(upLoadImageHighDefinitionVo);
            upLoadMainAndThumbnailImageVo.setUpLoadImageBoSuperDefinition(upLoadImageSuperDefinitionVo);
            return upLoadMainAndThumbnailImageVo;
        }catch (Exception e){
            log.error("上传失败",e);
        }
        throw new BusinessException("图片上传失败");
    }

    @Override
    public UpLoadImageVo upLoadMainImage(UploadFileInput uploadFileInput) {
        FileInterface fileInterface = fileFactoryInterface.getFileInterface(uploadFileInput.getBucketNameEnum());

        UpLoadImageInput upLoadImageInput = BeanUtil.copyProperties(uploadFileInput,UpLoadImageInput.class);
        //上传图片
        UpLoadImageOutput upLoadImageOutput = fileInterface.upLoadMainImage(upLoadImageInput);

        return BeanUtil.copyProperties(upLoadImageOutput, UpLoadImageVo.class);
    }

    @Override
    public UpLoadImageVo upLoadThumbnailImage(UploadFileInput uploadFileInput, Integer scaleSize) {
        FileInterface fileInterface = fileFactoryInterface.getFileInterface(uploadFileInput.getBucketNameEnum());

        UpLoadImageInput upLoadImageInput = BeanUtil.copyProperties(uploadFileInput,UpLoadImageInput.class);
        //上传图片
        UpLoadImageOutput upLoadImageOutput = fileInterface.upLoadThumbnailImage(upLoadImageInput,scaleSize);

        return BeanUtil.copyProperties(upLoadImageOutput, UpLoadImageVo.class);
    }

    @Override
    public void downFile(HttpServletResponse response, BucketNameEnum bucketNameEnum, String paths) {
        byte[] bytes = fileFactoryInterface.getFileInterface(bucketNameEnum).downFile(paths);

        try {
            IoUtil.write(response.getOutputStream(),true,bytes);
        } catch (Exception e) {
            throw new BusinessException("文件下载失败");
        }

    }
}
